<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.replace('/layout/user')"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="userList.photo"
            @click="imgClickFn"
          />
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="userList.name" @click="nameFn" />
      <van-cell
        title="生日"
        is-link
        :value="userList.birthday"
        @click="birthdayFn"
      />
    </van-cell-group>

    <van-dialog
      v-model="show"
      title="标题"
      show-cancel-button
      getContainer="body"
      :before-close="beforeCloseFn"
    >
      <van-field v-model="value" input-align="center" />
    </van-dialog>

    <van-popup
      v-model="isshow"
      position="bottom"
      round
      :style="{ height: '50%' }"
    >
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="confirmFn"
        @cancel="quitFn"
      />
    </van-popup>
  </div>
</template>

<script>
import {
  personalCenterAPI,
  modifyAvatarAPI,
  modifyPersonalInformationAPI
} from '@/api/index.js'
import { fromDate } from '@/utils/date.js'
import { mapMutations } from 'vuex'

import { Notify } from 'vant'
export default {
  name: 'UserEdit',
  data() {
    return {
      userList: {},
      show: false,
      isshow: false,
      value: '',
      minDate: new Date(1920, 0, 1),
      maxDate: new Date(),
      currentDate: new Date()
    }
  },
  methods: {
    ...mapMutations(['SET_UserFoto']),
    birthdayFn() {
      this.currentDate = new Date(this.userList.birthday)
      this.isshow = true
    },
    async confirmFn() {
      await modifyPersonalInformationAPI({
        birthday: fromDate(this.currentDate)
      })
      this.userList.birthday = fromDate(this.currentDate)
      this.isshow = false
    },
    quitFn() {
      this.isshow = false
    },
    async beforeCloseFn(action, done) {
      if (action === 'confirm') {
        const reg = /^[0-9A-Za-z\u4e00-\u9fa5]{1,7}$/
        if (reg.test(this.value) === true) {
          await modifyPersonalInformationAPI({ name: this.value })
          this.userList.name = this.value
          done(true)
        } else {
          Notify({ type: 'danger', message: '用户名非法！' })
          done(false)
        }
      } else {
        done(true)
      }
    },
    nameFn() {
      this.value = this.userList.name
      this.show = true
    },
    imgClickFn() {
      this.$refs.iptFile.click()
    },
    async onFileChange(e) {
      if (e.target.files.length === 0) return
      const theFd = new FormData()
      theFd.append('photo', e.target.files[0])
      const { data: res } = await modifyAvatarAPI(theFd)
      this.userList.photo = res.data.photo
      this.SET_UserFoto = res.data.photo
      // console.log(res)
    }
  },
  async created() {
    const { data: res } = await personalCenterAPI()
    // console.log(res)
    this.userList = res.data
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
